package com.dormitoryAdministratorSystem.ui.studentmanage;

import com.dormitoryAdministratorSystem.entity.page.PageBean;
import com.dormitoryAdministratorSystem.entity.student.StudentBean;
import com.dormitoryAdministratorSystem.service.student.impl.StudentSerImpl;
import com.dormitoryAdministratorSystem.ui.MainFrame;
import com.dormitoryAdministratorSystem.ui.roommanage.RoomManagePanel;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

public class StudentManagePanel extends JPanel {

	private LovoTable stuTab;

	private LovoButton stuInBtn;
	private LovoButton stuOutBtn;
	private LovoButton stuChgBtn;
	private LovoButton stuInfoBtn;
	private LovoButton preBtn;
	private LovoButton nextBtn;

	private JPanel queryPanel;
	private JLabel nameLab;
	private JLabel classLab;
	private JLabel roomLab;
	private JLabel  pageLab;
	private JTextField nameTxt;
	private JTextField classTxt;
	private JTextField roomTxt;
	private JButton queryBtn;

	private MainFrame frame;
	private RoomManagePanel rmp;

	private StudentSerImpl ssi = new StudentSerImpl();
	private Map<String,String> con = new HashMap<>();
	private PageBean<StudentBean> sPage;

	public StudentManagePanel(MainFrame frame, RoomManagePanel rmp) {
		this.frame = frame;
		this.rmp = rmp;
		this.setBackground(Color.WHITE);
		this.setLayout(null);
		this.initTable();
		this.initBtn();
		this.initQueryPanel();

	}

	private void initTable() {
		// TODO Auto-generated method stub
		this.stuTab = new LovoTable(this, new String[] { "学生姓名", "性别", "班级",
				"所在房间", "联系电话" }, new String[] {"sName", "gender", "classBean.cName", "roomBean.address", "phoneNum"},// 数组中应该是对应属性的属性名
				"sId");// 填入唯一标示属性
		this.stuTab.setSizeAndLocation(20, 0, 750, 300);
		sPage = ssi.showStusInfo(con);
		this.stuTab.updateLovoTable(sPage.getDates());
	}

	private void initBtn() {
		// TODO Auto-generated method stub

		this.preBtn = new LovoButton("上一页",10,320,this);
		this.nextBtn = new LovoButton("下一页",280,320,this);
		this.pageLab = new JLabel("页数" + sPage.getCurPage() + "/" + sPage.getTolSize() + ",共" + sPage.getTolNum() + "条"
				+ "每页" + sPage.getPageSize() + "条");
		this.pageLab.setBounds(105,320,300,20);
		this.add(this.pageLab);
		this.add(this.preBtn);
		this.add(this.nextBtn);

		this.stuInBtn = new LovoButton("学生入住", 80, 380, this);
		this.stuOutBtn = new LovoButton("学生退房", 240, 380, this);
		this.stuChgBtn = new LovoButton("学生换房", 80, 450, this);
		this.stuInfoBtn = new LovoButton("学生详细信息", 240, 450, this);

		this.stuInBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				new StudentInDialog(StudentManagePanel.this.frame);
				updateCon("","","","","");
				rmp.updatePage("","","","","","");
			}
		});

		this.stuOutBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int id = stuTab.getKeyByInt();
				if(id == -1){
					JOptionPane.showMessageDialog(null,"请先选择一个学生");
				}else {
						new StudentOutDialog(StudentManagePanel.this.frame, id);
					updateStudentAndRoom();
				}
			}
		});

		this.stuChgBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int id = stuTab.getKeyByInt();
				if(id == -1){
					JOptionPane.showMessageDialog(null,"请先选择学生");
				}else{
						new StudentChgDialog(StudentManagePanel.this.frame, id);
						updateStudentAndRoom();
				}
			}
		});
		
		this.stuInfoBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int id = stuTab.getKeyByInt();
				if(id == -1 ){
					JOptionPane.showMessageDialog(null,"请先选择一个学生");
				}else{
					new StudentShowDialog(StudentManagePanel.this.frame, id);
				}
			}
		});
		 //上一页
		this.preBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(sPage.getCurPage() == 1){
					JOptionPane.showMessageDialog(null,"没有上一页了");
				}else{
					sPage.setCurPage(sPage.getCurPage() - 1);
					updateCon(con.get("sname"),con.get("className"),con.get("rid"),sPage.getCurPage()+"", sPage.getPageSize()+"");
				}
			}
		});

		//下一页
		this.nextBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(sPage.getCurPage() == sPage.getTolSize()){
					JOptionPane.showMessageDialog(null,"没有下一页了");
				}else{
					sPage.setCurPage(sPage.getCurPage() + 1);
					updateCon(con.get("sname"),con.get("className"),con.get("rid"),
							sPage.getCurPage()+"", sPage.getPageSize()+"");
				}
			}
		});
	}

	private void initQueryPanel() {
		// TODO Auto-generated method stub
		this.queryPanel = new JPanel();
		this.queryPanel.setBorder(BorderFactory.createTitledBorder("查询学生信息"));
		this.queryPanel.setBackground(Color.WHITE);
		this.queryPanel.setLayout(null);
		this.queryPanel.setBounds(380, 310, 300, 180);
		this.add(this.queryPanel);

		// 标签
		this.nameLab = new JLabel("姓名");
		this.nameLab.setBounds(40, 20, 30, 30);
		this.queryPanel.add(this.nameLab);
		this.classLab = new JLabel("班级");
		this.classLab.setBounds(40, 60, 30, 30);
		this.queryPanel.add(this.classLab);
		this.roomLab = new JLabel("房间");
		this.roomLab.setBounds(40, 100, 30, 30);
		this.queryPanel.add(this.roomLab);

		// 文本框
		this.nameTxt = new JTextField();
		this.nameTxt.setBounds(80, 27, 140, 20);
		this.queryPanel.add(this.nameTxt);
		this.classTxt = new JTextField();
		this.classTxt.setBounds(80, 67, 140, 20);
		this.queryPanel.add(this.classTxt);
		this.roomTxt = new JTextField();
		this.roomTxt.setBounds(80, 107, 140, 20);
		this.queryPanel.add(this.roomTxt);

		// 查询按钮
		this.queryBtn = new JButton("查询");
		this.queryBtn.setBounds(200, 140, 80, 20);
		this.queryPanel.add(this.queryBtn);

		this.queryBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				updateCon(nameTxt.getText(), classTxt.getText(), roomTxt.getText(), "", ""
						);
			}
		});
	}

	private  void updateCon(String sname, String className, String rid, String curPage, String pageSize){
		con.put("sname", sname);
		con.put("className", className);
		con.put("rid", rid);
		con.put("curPage", curPage);
		con.put("pageSize", pageSize);
		sPage = ssi.showStusInfo(con);
		this.stuTab.updateLovoTable(sPage.getDates());
		this.pageLab.setText("页数" + sPage.getCurPage() + "/" + sPage.getTolSize() + ",共" + sPage.getTolNum() + "条"
				+ "每页" + sPage.getPageSize() + "条"
			);
	}

	private void updateStudentAndRoom(){
		updateCon(con.get("sname"),con.get("className"),con.get("rid"),sPage.getCurPage()+"",
				sPage.getPageSize()+"");
		rmp.updatePage("","","","","","");
	}

}
